package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import utils.Criterion;
import utils.Expert;
import utils.Risk;
import actions.CompetenceOfExpertFiller;
import actions.Conclusion;
import actions.ExpertCreator;
import actions.ExpertCriterionCreator;
import actions.ExpertCriterionFiller;
import actions.RiskCriterionCreator;
import actions.RiskCriterionFiller;
import actions.RiskEstimation;
import actions.RiskFiller;
import actions.WizardStep;

/**
 * 
 * @author dernasherbrezon
 *
 */
public class MainFrame extends JFrame
{
	int currentStep = 0;
	ArrayList<WizardStep> steps = new ArrayList<WizardStep>();
	
	public ArrayList<Expert> experts = new ArrayList<Expert>();
	public ArrayList<Criterion> expertCriterions = new ArrayList<Criterion>();
	public ArrayList<Criterion> riskCriterions = new ArrayList<Criterion>();
	public ArrayList<Risk> risks = new ArrayList<Risk>();
	public ArrayList<Risk> result = new ArrayList<Risk>();
	
	private JPanel initialButton;
	private JPanel bottom;
	private JPanel curCenterPanel;
	
	private JButton back;
	private JButton next;
	
	public MainFrame()
	{
		setTitle("Экспертиза рисков");
		
		getContentPane().setLayout(new BorderLayout());
		curCenterPanel = new WelcomeScreen();
		getContentPane().add(curCenterPanel,BorderLayout.CENTER);
		bottom = createBottom();
		getContentPane().add(bottom,BorderLayout.SOUTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		int resX = getGraphicsConfiguration().getBounds().width;
		int resY = getGraphicsConfiguration().getBounds().height;
		setBounds(resX/2 - getPreferredSize().width/2, resY/2 - getPreferredSize().height/2,0,0);
		setSize(new Dimension(600,500));
		setVisible(true);
	}
	
	private JPanel createInitialButton()
	{
		JPanel retValue = new JPanel();
		retValue.add(new JButton(new AbstractAction("Начать")
		{
			public void actionPerformed(ActionEvent arg0)
			{
				bottom.remove(initialButton);
				bottom.add(createControlButtons(),BorderLayout.CENTER);
				
				initStepPanels();

				setCurrentStep();
				
				MainFrame.this.validate();
				MainFrame.this.repaint();
			}
		}));
		return retValue;
	}
	
	private JPanel createControlButtons()
	{
		JPanel retValue = new JPanel();
		back = new JButton(new AbstractAction("Назад")
		{
			public void actionPerformed(ActionEvent arg0)
			{
				currentStep--;
				setCurrentStep();
			}
		});
		back.setEnabled(false);
		
		next = new JButton(new AbstractAction("Вперёд")
		{
			public void actionPerformed(ActionEvent arg0)
			{
				try
				{
					WizardStep curStep = steps.get(currentStep);
					curStep.checkInputValues();
					curStep.makeActions();
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,e.getMessage(),"Ошибка!",JOptionPane.ERROR_MESSAGE);
					return;
				}
				currentStep++;
				setCurrentStep();
			}
		});
		
		retValue.add(back);
		retValue.add(next);
		return retValue;
	}
	
	private JPanel createBottom()
	{
		JPanel retValue = new JPanel(new BorderLayout());
		initialButton = createInitialButton(); 
		retValue.add(initialButton,BorderLayout.CENTER);
		//retValue.add(createCopyright(),BorderLayout.SOUTH);
		return retValue;
	}
	
	private void initStepPanels()
	{
		ExpertCreator ec = new ExpertCreator(MainFrame.this.experts);
		steps.add(ec);
		//steps.add(new Task());
		steps.add(new ExpertCriterionCreator(this));
		steps.add(new ExpertCriterionFiller(this));
		steps.add(new CompetenceOfExpertFiller(this));
		steps.add(new RiskFiller(this));
		steps.add(new RiskCriterionFiller(this.riskCriterions));
		steps.add(new RiskCriterionCreator(this));
		steps.add(new RiskEstimation(this));
		steps.add(new Conclusion(this));
	}
	
	private void calculateEnableForControlButtons()
	{
		if(currentStep<1)
		{
			back.setEnabled(false);
			next.setEnabled(true);
		} else if(currentStep==steps.size()-1)
		{
			back.setEnabled(true);
			next.setEnabled(false);
		}
		else
		{
			back.setEnabled(true);
			next.setEnabled(true);
		}
	}
	
	private void setCurrentStep()
	{
		getContentPane().remove(curCenterPanel);
		steps.get(currentStep).loadStep();
		curCenterPanel = steps.get(currentStep);
		
		getContentPane().add(curCenterPanel);
		getContentPane().validate();
		getContentPane().repaint();
		
		calculateEnableForControlButtons();
	}
}
